package com.junzerg.tooltip.component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.junzerg.tooltip.constant.Constant;
import com.junzerg.tooltip.controller.request.ConvRequest;
import com.junzerg.tooltip.entity.dao.ConfigDao;
import com.junzerg.tooltip.entity.dao.ConvAmapAddressDao;
import com.junzerg.tooltip.entity.dao.ConvDao;
import com.junzerg.tooltip.entity.dto.ConvDto;
import com.junzerg.tooltip.service.IConfigService;
import com.junzerg.tooltip.service.IConvAmapAddressService;
import com.junzerg.tooltip.service.IConvService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pengjz <br>
 * @version 1.0 <br>
 * @description ConvComponent <br>
 * @date 2022/4/7 13:45 <br>
 */
@Component
@Slf4j
public class ConvComponent {

    @Resource
    private IConvService convService;

    @Resource
    private IConfigService configService;

    @Resource
    private IConvAmapAddressService convAmapAddressService;

    public List<ConvDto> searchByAddress(ConvRequest request) {
        return convService.list(
                new QueryWrapper<ConvDao>()
                        .like("address", request.getQ())
                .orderByDesc("`distinct`", "`report_date`")
        ).stream().map(
                ConvDao::buildDto
        ).collect(Collectors.toList());
    }

    public List<String> getConvAnnouncement() {
        return configService.list(
                new QueryWrapper<ConfigDao>().eq(
                        "config_key", "conv_announcement"
                )
        ).stream().map(ConfigDao::getConfigValue).collect(Collectors.toList());
    }

    public List<ConvAmapAddressDao> getInputTips(String q) {
        return convAmapAddressService.list(
                new QueryWrapper<ConvAmapAddressDao>()
                        .like("address_name", q)
                        .or()
                        .like("conv_address", q)
                        .last("limit 10")
        );
    }
}
